package myapps.ttclient.activity.connection;

import myapps.ttclient.connection.Connection;
import myapps.ttclient.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public abstract class ConnectionEditActivity extends Activity implements OnClickListener
{
	public final static int TYPE_NONE = -1;
	public final static int TYPE_NEW = 0;
	public final static int TYPE_EDIT = 1;

	protected int type = TYPE_NONE;
	
	public static Connection connectionParam;
	
	private Connection connection;
	
	private Button save;
	
	private Button cancel;
	
	private EditText name;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.connection = connectionParam;
		
		this.save = (Button) this.findViewById(R.id.save);
		this.save.setOnClickListener(this);
		
		this.cancel = (Button) this.findViewById(R.id.cancel);
		this.cancel.setOnClickListener(this);
		
		
		this.name = (EditText) this.findViewById(R.id.name);
		
		this.name.setText(this.connection.getName());
	}
	
	public void getCommonIntentContent() {
		try {
			Intent intent = this.getIntent();

			if(intent != null) {
				String str = intent.getStringExtra("type");
				
				if("new".equals(str) == true) {
					type = TYPE_NEW;
					
				} else if ("edit".equals(str) == true) {
					type = TYPE_EDIT;
				
				} else { 
					type = TYPE_NONE;	

					setResult(Activity.RESULT_CANCELED);

					this.finish();
				}
			} else {
				type = TYPE_NONE;	

				setResult(Activity.RESULT_CANCELED);

				this.finish();
			}
		} catch (Exception ex) {
			type = TYPE_NONE;	

			setResult(Activity.RESULT_CANCELED);

			this.finish();
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	public void onClick(View v) {
		if (v == this.save) {
			onSave();
			
			setResult(Activity.RESULT_OK);
			
			this.finish();
		}
		
		if (v == this.cancel) {
			setResult(Activity.RESULT_CANCELED);
			
			this.finish();
		}
	}
	
	public void onSave() {
		this.connection.setName(this.name.getText().toString());
	}
}
